//
//  QKeyBoardView.m
//  QoilPublic
//
//  Created by 杨凯 on 16/8/16.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QKeyBoardView.h"

@implementation QKeyBoardView{

    NSArray *_titleArr;
}

- (instancetype)initWithFrame:(CGRect)frame andBlock:(ClickBlock)block{

    self = [super initWithFrame:frame];
    if(self){
    
        _titleArr = @[@"1",@"2",@"3",@"",@"4",@"5",@"6",@"",@"7",@"8",@"9",@"",@".",@"0",@"",@""];
        _cblock = block;
        [self createUI];
    }

    return self;
}

- (void)createUI{

    int w = kScreenWidth/4;
    int h = 50;
    for(int i=0; i<16; ++i){

        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i%4*w, i/4*h, w, h)];
        [button setBackgroundImage:[QColorUtility buttonImageFromColor:[UIColor colorWithWhite:0 alpha:0.8] rect:CGRectMake(0, 0, kScreenWidth/4, 50)] forState:UIControlStateNormal];
        [button setBackgroundImage:[QColorUtility buttonImageFromColor:[UIColor colorWithRed:80/255.0 green:80/255.0 blue:80/255.0 alpha:1] rect:CGRectMake(0, 0, kScreenWidth/4, 50)] forState:UIControlStateHighlighted];
        button.layer.borderWidth = 0.3;
        button.layer.borderColor = [QColorUtility defaultBackGroundColor].CGColor;
        button.tag = i;
        [button setTitle:_titleArr[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        if(button.tag == 3 || button.tag == 7 || button.tag == 11 || button.tag == 15){
            button.hidden = YES;
        }else{
            button.hidden = NO;
        }
        
        if(button.tag == 14){
            [button setImage:[UIImage imageNamed:@"counter_image"] forState:UIControlStateNormal];
        }
    }
    
    
    for (int i=0; i<2; ++i) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(w*3, i%2*h*2, w+2, h*2)];
        [button setBackgroundImage:[QColorUtility buttonImageFromColor:[UIColor colorWithWhite:0 alpha:0.8] rect:CGRectMake(0, 0, kScreenWidth/4, 50)] forState:UIControlStateNormal];
        [button setBackgroundImage:[QColorUtility buttonImageFromColor:[UIColor colorWithRed:80/255.0 green:80/255.0 blue:80/255.0 alpha:1] rect:CGRectMake(0, 0, kScreenWidth/4, 50)] forState:UIControlStateHighlighted];
        button.layer.borderWidth = 0.3;
        button.layer.borderColor = [QColorUtility defaultBackGroundColor].CGColor;
        button.titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        if(i == 0){
            button.tag = 16;
            [button setImage:[UIImage imageNamed:@"delete_image"] forState:UIControlStateNormal];
        }else if (i == 1){
            button.tag = 17;
            [button setTitle:@"确定" forState:UIControlStateNormal];
        }
        [self addSubview:button];
    }
}

- (void)buttonAction:(UIButton *)button{
    if(_cblock){
        _cblock(button.tag);
    }
}

@end
